package com.ruoyi.project.warehouse.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.warehouse.domain.*;
import com.ruoyi.project.warehouse.mapper.OutRyDepotItemMapper;
import com.ruoyi.project.warehouse.mapper.RyDepotItemMapper;
import com.ruoyi.project.warehouse.mapper.RyInventoryCurrentStockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.warehouse.mapper.RyDepotRecordAuditMapper;
import com.ruoyi.project.warehouse.service.IRyDepotRecordAuditService;

/**
 * 出入库审核Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-17
 */
@Service
public class RyDepotRecordAuditServiceImpl implements IRyDepotRecordAuditService
{
    @Autowired
    private RyDepotRecordAuditMapper ryDepotRecordAuditMapper;

    @Autowired
    private RyInventoryCurrentStockMapper ryInventoryCurrentStockMapper;

    @Autowired
    private RyDepotItemMapper ryDepotItemMapper;

    @Autowired
    private OutRyDepotItemMapper outRyDepotItemMapper;

    /**
     * 查询出入库审核
     *
     * @param id 出入库审核主键
     * @return 出入库审核
     */
    @Override
    public RyDepotRecordAudit selectRyDepotRecordAuditById(Long id)
    {
        return ryDepotRecordAuditMapper.selectRyDepotRecordAuditById(id);
    }

    /**
     * 查询出入库审核列表
     *
     * @param ryDepotRecordAudit 出入库审核
     * @return 出入库审核
     */
    @Override
    public List<RyDepotRecordAuditVo> selectRyDepotRecordAuditList(RyDepotRecordAudit ryDepotRecordAudit)
    {
        return ryDepotRecordAuditMapper.selectRyDepotRecordAuditList(ryDepotRecordAudit);
    }

    /**
     * 新增出入库审核
     *
     * @param ryDepotRecordAuditVo,partName 出入库审核
     * @return 结果
     */
    @Override
    public int insertRyDepotRecordAudit(RyDepotRecordAuditVo ryDepotRecordAuditVo,String partName)
    {
        //获取当前用户id（即当前修改人id）
        Long userId = SecurityUtils.getUserId();
        System.out.println("ProjectId======="+ryDepotRecordAuditVo.getProjectId());
        RyDepotRecordAudit ryDepotRecordAudit = new RyDepotRecordAudit();
        ryDepotRecordAudit.setInventoryId(ryDepotRecordAuditVo.getInventoryId());
        ryDepotRecordAudit.setDepotId(ryDepotRecordAuditVo.getDepotId());
        ryDepotRecordAudit.setBeforeNum(ryDepotRecordAuditVo.getBeforeNum());
        ryDepotRecordAudit.setAfterNum(ryDepotRecordAuditVo.getAfterNum());
        ryDepotRecordAudit.setBeforeMoney(ryDepotRecordAuditVo.getBeforeMoney());
        ryDepotRecordAudit.setAfterMoney(ryDepotRecordAuditVo.getAfterMoney());
        ryDepotRecordAudit.setDepotitemId(ryDepotRecordAuditVo.getDepotitemId());
        ryDepotRecordAudit.setAuditStatus(0);
        ryDepotRecordAudit.setCreateDate(new Date());
        ryDepotRecordAudit.setCreatorId(userId);
        ryDepotRecordAudit.setPartName(partName);
        ryDepotRecordAudit.setRemark(ryDepotRecordAuditVo.getRemark());
        ryDepotRecordAudit.setProjectId(ryDepotRecordAuditVo.getProjectId());

        return ryDepotRecordAuditMapper.insertRyDepotRecordAudit(ryDepotRecordAudit);
    }

    /**
     * 修改出入库审核
     *
     * @param ryDepotRecordAudit 出入库审核
     * @return 结果
     */
    @Override
    public int updateRyDepotRecordAudit(RyDepotRecordAudit ryDepotRecordAudit)
    {
        return ryDepotRecordAuditMapper.updateRyDepotRecordAudit(ryDepotRecordAudit);
    }

    /**
     * 批量删除出入库审核
     *
     * @param ids 需要删除的出入库审核主键
     * @return 结果
     */
    @Override
    public int deleteRyDepotRecordAuditByIds(Long[] ids)
    {
        return ryDepotRecordAuditMapper.deleteRyDepotRecordAuditByIds(ids);
    }

    /**
     * 删除出入库审核信息
     *
     * @param id 出入库审核主键
     * @return 结果
     */
    @Override
    public int deleteRyDepotRecordAuditById(Long id)
    {
        return ryDepotRecordAuditMapper.deleteRyDepotRecordAuditById(id);
    }

    /**
     * 审核出入库修改信息
     *
     * @param ids 审核出入库修改信息
     * @return 结果
     */
    @Override
    public int auditRyDepotRecord(Long[] ids) {


        RyDepotRecordAudit ryDepotRecordAudit = ryDepotRecordAuditMapper.selectRyDepotRecordAuditById(ids[0]);

        RyInventoryCurrentStock ryInventoryCurrentStock = new RyInventoryCurrentStock();
        ryInventoryCurrentStock.setDepotId(ryDepotRecordAudit.getDepotId());
        ryInventoryCurrentStock.setInventoryId(ryDepotRecordAudit.getInventoryId());
        List<RyInventoryCurrentStock> ryInventoryCurrentStocks = ryInventoryCurrentStockMapper.selectRyInventoryCurrentStockList(ryInventoryCurrentStock);

        //当前库存数量
        Double currentNumber = ryInventoryCurrentStocks.get(0).getCurrentNumber();
        Double beforeNum = ryDepotRecordAudit.getBeforeNum();
        Double afterNum = ryDepotRecordAudit.getAfterNum();

        //判断当前库存数是否足够出库
        String partName = ryDepotRecordAudit.getPartName();//出入库模块名
        if(afterNum!=null){
            if(partName.equals("出库") && afterNum>(currentNumber+beforeNum)){
                System.out.println("出库时数量不足从这走");
                //出库数量不足不足以审核时，将数据库审核状态改为2：作废，并给出作废原因
                ryDepotRecordAudit.setAuditStatus(2);
                ryDepotRecordAudit.setRemark("物品数量修改后出库时库存不足");
                ryDepotRecordAuditMapper.updateRyDepotRecordAudit(ryDepotRecordAudit);
                return -1;
            }
        }


        //审核通过，把原出库入库记录改成修改后的数量并调整对应产品的当前总库存
        if(partName.equals("出库")){
            //把原出库记录的数量和金额改为修改后的值
            OutRyDepotItem outRyDepotItem = outRyDepotItemMapper.selectOutRyDepotItemById(ryDepotRecordAudit.getDepotitemId());
            if(afterNum!=null){
                outRyDepotItem.setOperNumber(afterNum);

                //根据修改后的出库数量调整当前总库存数量
                if(afterNum!=null){
                    ryInventoryCurrentStocks.get(0).setCurrentNumber(currentNumber + beforeNum - afterNum);
                    int i1 = ryInventoryCurrentStockMapper.updateRyInventoryCurrentStock(ryInventoryCurrentStocks.get(0));
                    if(i1<=0){
                        return 0;
                    }
                }
            }
            outRyDepotItem.setAllPrice(ryDepotRecordAudit.getAfterMoney());
            outRyDepotItem.setRemark(ryDepotRecordAudit.getRemark());
            outRyDepotItem.setUpdateTime(ryDepotRecordAudit.getCreateDate());
            outRyDepotItem.setTenantId(ryDepotRecordAudit.getProjectId());
            int i = outRyDepotItemMapper.updateOutRyDepotItem(outRyDepotItem);
            if(i<=0){
                return 0;
            }



        } else if(partName.equals("入库")){
            //把原入库记录的数量和金额改为修改后的值
            RyDepotItem ryDepotItem = ryDepotItemMapper.selectRyDepotItemById(ryDepotRecordAudit.getDepotitemId());
            if(afterNum!=null){
                ryDepotItem.setOperNumber(afterNum);

                //根据修改后的入库数量调整当前总库存数量
                ryInventoryCurrentStocks.get(0).setCurrentNumber(currentNumber - beforeNum + afterNum);
                int i1 = ryInventoryCurrentStockMapper.updateRyInventoryCurrentStock(ryInventoryCurrentStocks.get(0));
                if(i1<=0){
                    return 0;
                }
            }
            ryDepotItem.setAllPrice(ryDepotRecordAudit.getAfterMoney());
            ryDepotItem.setRemark(ryDepotRecordAudit.getRemark());
            ryDepotItem.setUpdateTime(ryDepotRecordAudit.getCreateDate());
            int i = ryDepotItemMapper.updateRyDepotItem(ryDepotItem);
            if(i<=0){
                return 0;
            }



        }

        ryDepotRecordAudit.setAuditStatus(1);
        ryDepotRecordAudit.setAuditDate(new Date());
        ryDepotRecordAudit.setAuditorId(SecurityUtils.getUserId());
        int i = ryDepotRecordAuditMapper.updateRyDepotRecordAudit(ryDepotRecordAudit);
        if(i<=0){
            return 0;
        }

        return i;
    }

}
